package cn.shopex.multiimagepreview.app;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.shopex.multiimagepreview.R;

/**
 * Created by p on 2016/11/11.
 */
public class GalleryFragment extends Fragment implements ProgressImageView.ImageViewOnClick {
    private GalleryActivity mActivity;
    private View rootView;
    private ProgressImageView progressImageView;
    private String  mUrl="";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (GalleryActivity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseArguments();
        setRetainInstance(true);
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_browse_picture, container, false);
        progressImageView = (ProgressImageView) rootView.findViewById(R.id.progress_img);
        progressImageView.load(mUrl, GalleryFragment.this);
        progressImageView.setImageViewOnClick(this);
        return rootView;
    }

    private void parseArguments() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mUrl = bundle.getString("url");
        }
    }

    @Override
    public void onImageClick(View v) {
        mActivity.switchToolbar();
    }

    public static GalleryFragment newInstance(String url) {
        GalleryFragment fragment = new GalleryFragment();
        Bundle args = new Bundle();
        args.putString("url", url);
        fragment.setArguments(args);
        return fragment;
    }
}
